package com.csjung.games.gamenori.client.ui;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.Widget;

public class WidgetWrapper extends Composite implements HasAlignment {

  private HorizontalAlignmentConstant alignLeft;
  private VerticalAlignmentConstant alignTop;
  
  public WidgetWrapper(Widget widget) {
    this(widget, HasAlignment.ALIGN_CENTER, HasAlignment.ALIGN_MIDDLE);
  }

  public WidgetWrapper(Widget widget, HorizontalAlignmentConstant hAlign) {
    this(widget, hAlign, HasAlignment.ALIGN_MIDDLE);
  }

  public WidgetWrapper(Widget widget, VerticalAlignmentConstant vAlign) {
    this(widget, HasAlignment.ALIGN_CENTER, vAlign);
  }

  public WidgetWrapper(Widget widget, HorizontalAlignmentConstant hAlign,
      VerticalAlignmentConstant vAlign) {
    Grid grid = new Grid(1, 1);
    grid.setBorderWidth(0);
    grid.setCellPadding(0);
    grid.setCellSpacing(0);
    this.alignLeft = hAlign;
    this.alignTop = vAlign;
    setAlignment(grid);
    grid.setWidget(0, 0, widget);
    initWidget(grid);
  }
  
  public Grid getWidget() {
    return (Grid) super.getWidget();
  }
  
  @Override
  public HorizontalAlignmentConstant getHorizontalAlignment() {
    return alignLeft;
  }

  @Override
  public void setHorizontalAlignment(HorizontalAlignmentConstant align) {
    alignLeft = align;
    setAlignment(getWidget());
    
  }

  @Override
  public VerticalAlignmentConstant getVerticalAlignment() {
    return alignTop;
  }

  @Override
  public void setVerticalAlignment(VerticalAlignmentConstant align) {
    alignTop = align;
    setAlignment(getWidget());
    
  }
  
  private void setAlignment(Grid grid) {
    grid.getCellFormatter().setAlignment(0, 0, alignLeft, alignTop);
  }

}
